#include "dlgchart.h"
#include "ui_dlgchart.h"
#include <QWebEngineView>
#include <QWebChannel>
#include <QJsonObject>
#include <QDir>
#include <QDebug>
#include "bridge.h"
#include <QString>
QWebEngineView *pview;

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    QWebChannel *channel = new QWebChannel(this);
    channel->registerObject("bridge", (QObject*)bridge::instance());

    pview = new QWebEngineView();
    pview->page()->setWebChannel(channel);
    pview->page()->load(QUrl(QString("file:///%1/%2").arg(QApplication::applicationDirPath()).arg("../index.html")));

    ui->viewLayout->addWidget(pview);
    connect(this, SIGNAL(sendjsonobj2js(QJsonObject)), bridge::instance(), SIGNAL(sendjsonobj2js(QJsonObject)));
    connect(this, SIGNAL(sendjsonarray2js(QJsonArray)), bridge::instance(), SIGNAL(sendjsonarray2js(QJsonArray)));
    connect(this, SIGNAL(sendstring2js(QString)), bridge::instance(), SIGNAL(sendstring2js(QString)));
//    connect(ui->pbAlert, &QPushButton::clicked, [this]() {
//        m_view->page()->runJavaScript("showAlert()");
//    });
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_pbAlert_clicked()
{
    //QWebEngineView *pview = (QWebEngineView *)ui->viewLayout->;
   // pview->page()->runJavaScript("showAlert()");
   QJsonArray json;
   QString name = "ABCDEFGK";
   for (int i = 0; i < 5; i++)
   {
	   QJsonObject OBJ;
	   OBJ.insert("value", qrand());
	   OBJ.insert("name", QString(name.at(i)));
	   json.append(OBJ);
   }

   qDebug()<<json;

    sendjsonarray2js(json);
}
